package com.atguigu.realtime.app.dws;

import com.atguigu.realtime.app.BaseSqlApp;
import com.atguigu.realtime.bean.KeyWordStats;
import com.atguigu.realtime.function.IkAnalyzer;
import com.atguigu.realtime.function.KeyWordProduct;
import com.atguigu.realtime.util.FlinkSinkUtil;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

import java.time.ZoneOffset;

import static com.atguigu.realtime.commont.Constant.TOPIC_DWS_PRODUCT_STATS;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/12/3 9:10
 */
public class DwProductKeyWordApp extends BaseSqlApp {
    public static void main(String[] args) {
        new DwProductKeyWordApp().init(4004, 1, "DwProductKeyWordApp");
        
    }
    
    @Override
    protected void run(StreamTableEnvironment tenv) {
        tenv.getConfig().setLocalTimeZone(ZoneOffset.ofHours(8));
        // 1. 建一张动态表与topic关联
        tenv.executeSql("create table product(" +
                            "   stt string, " +
                            "   edt string, " +
                            "   sku_name string, " +
                            "   click_ct bigint," +
                            "   order_ct bigint," +
                            "   cart_ct bigint " +
                            ")with(" +
                            "   'connector' = 'kafka'," +
                            "   'properties.bootstrap.servers' = 'hadoop162:9092,hadoop163:9092, hadoop164:9092'," +
                            "   'properties.group.id' = 'DwProductKeyWordApp'," +
                            "   'topic' = '" + TOPIC_DWS_PRODUCT_STATS + "'," +
                            "   'scan.startup.mode' = 'earliest-offset', " +
                            "   'format' = 'json' " +
                            ")");
        
        // 1. 过滤出需要的数据: 三个ct至少一个不为0的数据
        Table t1 = tenv.sqlQuery("select" +
                                     " * " +
                                     "from product " +
                                     "where click_ct > 0 " +
                                     "or order_ct > 0 " +
                                     "or cart_ct > 0");
        tenv.createTemporaryView("t1", t1);
        
        // 2. 对sku_name进行分词
        tenv.createTemporaryFunction("ik_analyzer", IkAnalyzer.class);
        Table t2 = tenv.sqlQuery("select" +
                                     "  stt, " +
                                     "  edt, " +
                                     " keyword, " +
                                     " click_ct, " +
                                     " order_ct," +
                                     " cart_ct " +
                                     "from t1, lateral table(ik_analyzer(sku_name)) ");
        tenv.createTemporaryView("t2", t2);
        
        // 3. 把三个ct(3列)变成3行
        tenv.createTemporaryFunction("keyword_product", KeyWordProduct.class);
        Table t3 = tenv.sqlQuery("select" +
                                     " stt, " +
                                     " edt, " +
                                     " keyword," +
                                     " source, " +
                                     " ct " +
                                     "from t2 " +
                                     "join lateral table(keyword_product(click_ct, order_ct, cart_ct)) on true");
        tenv.createTemporaryView("t3", t3);
        // 4. 按照窗口 关键词 和source进行聚合
        Table result = tenv.sqlQuery("select" +
                                         " stt, edt, keyword, source, " +
                                         " sum(ct) ct, " +
                                         " unix_timestamp() * 1000 ts " +
                                         "from t3 " +
                                         "group by stt, edt, keyword, source");
        
        // 5. 写入到 ClickHouse 中
        tenv
            .toRetractStream(result, KeyWordStats.class)
            .filter(t -> t.f0)
            .map(t -> t.f1)
            .addSink(FlinkSinkUtil.getClickHouseSink("gmall2021", "keyword_stats_2021", KeyWordStats.class));
        
    }
}
